package com.shareach.http.shop.rpc;

import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shareach.domain.BankCollectDomain;
import com.shareach.http.shop.parameter.BankCollectParameter;
import com.shareach.service.BankCollectService;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.basic.IAskParameter;
import com.shareach.web.basic.IAskRPC;

@Service("bankUploadRpc")
public class BankUpload implements IAskRPC {
	@Autowired
	private BankCollectService bcollectService;
	// @Autowired
	// private SnatchRDao snatchRDao;
	// @Autowired
	// private MatchRDao matchRDao;
	final Logger logger = Logger.getLogger("quartz");

	@Override
	public Answer<?> run(Ask ask, String ip) throws Exception {
		BankCollectParameter p = (BankCollectParameter) getParameterMap(ask);
		Answer<Long> result = this.getAnswer();

		BankCollectDomain domain = new BankCollectDomain();

		if (StringUtils.isEmpty(p.url)) {
			result.setCode(-1);
			result.setMsg("该条数据没有url,不处理");
			logger.info("该条数据没有url,不处理:::" + ask.getSn());
			return result;
		}
		// 用来判断是否采集
		// snatchRDao.setSnatchedSiteId("bankCard", p.site, p.siteFromId);
		domain.setBank(p.bank);
		domain.setPageUrl(p.pageUrl);
		domain.setUrl(p.url);
		domain.setTitle(p.title); 
		domain.setStatus(0);
		bcollectService.saveOrUpdate(domain);
		// String retCode = (String)
		// retMap.get(Globals.GLOBAL_SERVICE_RET_CODE);
		// // 匹配队列使用
		// if (retCode.equals(Globals.GLOBAL_SERVICE_CODE_NOTEXIST)) {
		// matchRDao.addDiscountShop(DiscountType.Card, (Long)
		// retMap.get(Globals.GLOBAL_SERVICE_RET_ID));
		// }

		return result;
	}

	@Override
	public Answer<Long> getAnswer() {
		return new Answer<Long>();
	}

	@Override
	public IAskParameter getParameterMap(Ask ask) {
		BankCollectParameter parameter = new BankCollectParameter();
		HashMap<String, Object> map = ask.getRequest();
		parameter.parseFromMap(map);
		return parameter;
	}
}
